/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.MemoriaAplicInterface;
import br.gov.cemaden.library.model.smp.ExecAplic;
import br.gov.cemaden.library.model.smp.MemoriaAplic;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class MemoriaAplicDao extends GenericDao implements MemoriaAplicInterface {

    private Session session;

    public MemoriaAplicDao(Session session) {
        this.session = session;
    }

    public List<MemoriaAplic> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findAll");
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public MemoriaAplic findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        MemoriaAplic memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findById").setParameter("id", id);
            memoriaAplic = (MemoriaAplic) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByHeapInit(Double heapInit) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByHeapInit").setParameter("heapInit", heapInit);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByHeapInit: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByHeapUsed(Double heapUsed) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByHeapUsed").setParameter("heapUsed", heapUsed);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByHeapUsed: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByHeapCommitted(Double heapCommitted) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByHeapCommitted").setParameter("heapCommitted", heapCommitted);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByHeapCommitted: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByHeapMax(Double heapMax) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByHeapMax").setParameter("heapMax", heapMax);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByHeapMax: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByPermGenInit(Double permGenInit) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByPermGenInit").setParameter("permGenInit", permGenInit);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByPermGenInit: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByPermGenUsed(Double permGenUsed) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByPermGenUsed").setParameter("permGenUsed", permGenUsed);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByPermGenUsed: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByPermGenCommitted(Double permGenCommitted) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByPermGenCommitted").setParameter("permGenCommitted", permGenCommitted);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByPermGenCommitted: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByPermGenMax(Double permGenMax) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByPermGenMax").setParameter("permGenMax", permGenMax);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByPermGenMax: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public List<MemoriaAplic> findByExecAplic(ExecAplic execAplic) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<MemoriaAplic> memoriaAplic = null;
        try {
            Query query = session.getNamedQuery("MemoriaAplic.findByExecAplic").setParameter("execAplic", execAplic);
            memoriaAplic = (List<MemoriaAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaAplicFacade.findByExecAplic: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoriaAplic;
    }

    public MemoriaAplic insert(MemoriaAplic memoriaAplic) {
        try {
            saveOrUpdate(this.session, memoriaAplic);
        } catch (Exception ex) {
            Logger.getLogger(MemoriaAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return memoriaAplic;
    }

    public MemoriaAplic update(MemoriaAplic memoriaAplic) {
        try {
            saveOrUpdate(this.session, memoriaAplic);
        } catch (Exception ex) {
            Logger.getLogger(MemoriaAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return memoriaAplic;
    }

    public void delete(MemoriaAplic memoriaAplic) {
        try {
            remove(this.session, memoriaAplic);
        } catch (Exception ex) {
            Logger.getLogger(MemoriaAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
